package org.zjx.analysis.analyze.game;

import lombok.Getter;
import org.zjx.command.util.ImageUtil;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;


@Getter
public class AttributeAnalysis {
    public Map<String, Rectangle> imageMap = new HashMap<>();
    public static BufferedImage bloodImage = ImageUtil.createImage("iVBORw0KGgoAAAANSUhEUgAAADgAAAABCAYAAACL8217AAAAm0lEQVR42mOYoaPx/4OJxv8fEQH/f2zZ8P/HmhX/fyxZ8P9HT8f/Hw0N/3/MmPD/xxQgngDELQ0QsZqK/x+g+EVJwf8XBRn/P1RUQORbIPI/Cgr+f8gp+P8DKA/mg8Q7OhBmwDBIXwlUfQbQnJQECAaxcyBmgPkxERCcgCQP4ocE/H/h4vD/hY0FGP8Aij2wMPh/Q0Hi/w4Bgf8AoZebpb0ZYncAAAAASUVORK5CYII=");
    public static BufferedImage emptyImage = ImageUtil.createImage("iVBORw0KGgoAAAANSUhEUgAAADgAAAABCAYAAACL8217AAAAIklEQVR42mMwMDH4P2PGjP8b1mz4v2HDMMJbNvxfMGfBfwCj6KlROUAz7AAAAABJRU5ErkJggg==");

    protected final BufferedImage screenshot;

    private int hpPercentageRole; //血量百分比
    private int injuryPercentageRole; //伤势百分比
    private int mpPercentageRole; //蓝量百分比
    private int ragePercentageRole; //愤怒百分比
    private int exePercentageRole; //经验百分比

    private int hpPercentagePet; //血量百分比
    private int mpPercentagePet; //蓝量百分比
    private int exePercentagePet; //经验百分比


    public AttributeAnalysis(BufferedImage screenshot) {
        this.screenshot = copy(screenshot);
        initResources();
        analysis();
    }

    public void initResources() {
        imageMap.put("人物血量", new Rectangle(735, 8, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("人物蓝量", new Rectangle(735, 20, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("人物愤怒", new Rectangle(735, 32, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("人物经验", new Rectangle(735, 44, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("宠物血量", new Rectangle(615, 8, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("宠物蓝量", new Rectangle(615, 20, bloodImage.getWidth(), bloodImage.getHeight()));
        imageMap.put("宠物经验", new Rectangle(615, 32, bloodImage.getWidth(), bloodImage.getHeight()));
    }


    private BufferedImage copy(BufferedImage screenshot) {
        // 创建副本，以避免对原图像产生影响
        BufferedImage copy = new BufferedImage(screenshot.getWidth(), screenshot.getHeight(), BufferedImage.TYPE_INT_ARGB);
        copy.getGraphics().drawImage(screenshot, 0, 0, null);
        return copy;
    }


    public Rectangle getOriginBounds() {
        return null;
    }

    public void analysis() {
        hpPercentagePet = checkAndUpdate(imageMap.get("宠物血量"), false);
        mpPercentagePet = checkAndUpdate(imageMap.get("宠物蓝量"), false);
        exePercentagePet = checkAndUpdate(imageMap.get("宠物经验"), false);
        hpPercentageRole = checkAndUpdate(imageMap.get("人物血量"), true);
        mpPercentageRole = checkAndUpdate(imageMap.get("人物蓝量"), false);
        injuryPercentageRole = checkAndUpdate(imageMap.get("人物血量"), false);
        ragePercentageRole = checkAndUpdate(imageMap.get("人物愤怒"), false);
        exePercentageRole = checkAndUpdate(imageMap.get("人物经验"), false);
        System.out.println("11");
    }

    // 通用检测更新方法
    private int checkAndUpdate(Rectangle rectangle, boolean isRoleBlood) {
        BufferedImage image = ImageUtil.cutImage(screenshot, rectangle);
        int count = 0;
        for (int i = emptyImage.getWidth() - 1; i >= 0; i--) {
            ;
            int imageColor = image.getRGB(i, 0);
            if (isRoleBlood) {
                int emptyColor = bloodImage.getRGB(i, 0);
                if (emptyColor != imageColor) count++;
                else break;
            } else {
                int emptyColor = emptyImage.getRGB(i, 0);
                if (emptyColor == imageColor) count++;
                else break;
            }
        }
        int value = (emptyImage.getWidth() - count) * 100;
        return value / emptyImage.getWidth();
    }

    private boolean checkPixelColor(Point p) {
        int color = screenshot.getRGB(p.x, p.y);
        return color == new Color(176, 176, 176).getRGB();
    }

}
